package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.chacha.v2.MainScreen;
import vn.com.vega.music.clientserver.JsonPlaylist;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.VegaLog;
import vn.com.vega.music.view.adapter.PlaylistListAdapter;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingAdapter;
import vn.com.vega.music.view.adapter.base.AutoLoadingListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class ListPlaylistFeatureFragment extends Fragment implements
		AutoLoadingListener, Const {
	private MainScreen mMainScreen;
	private Appinstant mApp;
	private PlaylistListAdapter mAdapter;
	private ListView mListPlaylist;
	private LoadAlbumFeature mLoader;
	private LoadingView mLoadingView;

	public static Fragment newInstance() {
		ListPlaylistFeatureFragment fragment = new ListPlaylistFeatureFragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater.inflate(R.layout.list_song_feature, null);
		mListPlaylist = (ListView) root.findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) root
				.findViewById(R.id.loadingView_loading_list_feature);
		mLoadingView.setRetryListener(retryListener);
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mMainScreen = (MainScreen) getActivity();
		mApp = (Appinstant) mMainScreen.getApplicationContext();
		// if (mApp.listSongFeature.size() > 0) {
		setAdapter();
		// }
	}

	private void setAdapter() {
		mAdapter = new PlaylistListAdapter(mMainScreen,
				mApp.listPlaylistFeature);
		mListPlaylist.setAdapter(new AutoLoadingAdapter(mAdapter, this));
		// mListPlaylist.setOnItemClickListener(onAlbumClickListener);
	}

	@Override
	public Activity getContext() {
		// TODO Auto-generated method stub
		return mMainScreen;
	}

	@Override
	public List<Object> getMoreData() {
		// TODO Auto-generated method stub
		if (mAdapter.getCount() >= LIST_ITEM_LIMIT) {
			return null;
		}
		List<Object> result = new ArrayList<Object>();
		JsonPlaylist jpl = JsonPlaylist
				.loadTopPlaylistList(mAdapter.getCount());
		if (jpl.isSuccess()) {
			result.addAll(jpl.playlists);
		}
		return result;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		VegaLog.e(SongListAdapter.class.getName(),
				"Resume list feature playlist");
		if (mAdapter.getCount() == 0) {
			loadListPlaylistFeature();
		} else {
			showListPlaylist();
		}
	}

	private void loadListPlaylistFeature() {
		if (mLoader != null && mLoader.getStatus() == Status.RUNNING) {
			showLoading();
		} else {
			if (mLoader != null) {
				mLoader.cancel(true);
			}
			mLoader = new LoadAlbumFeature(this);
			mLoader.execute();
		}
	}

	private void showLoading() {
		mListPlaylist.setVisibility(View.GONE);
		mLoadingView.startLoading();
	}

	private void showListPlaylist() {
		if (mAdapter.getCount() == 0) {
			mListPlaylist.setVisibility(View.GONE);
			mLoadingView.loadingFinish(false, "Danh sách playlist trống!");
		} else {
			mLoadingView.loadingFinish(true, null);
			mListPlaylist.setVisibility(View.VISIBLE);
		}
	}

	RetryListener retryListener = new RetryListener() {

		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			loadListPlaylistFeature();
		}
	};

	View.OnClickListener retryClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			loadListPlaylistFeature();
		}
	};

	// AdapterView.OnItemClickListener onAlbumClickListener = new
	// AdapterView.OnItemClickListener() {
	//
	// @Override
	// public void onItemClick(AdapterView<?> parent, View view, int position,
	// long id) {
	// // TODO Auto-generated method stub
	// Intent inten = new Intent(mMainScreen, PlaylistDetail.class);
	// String nameOfPlaylist = ListPlaylistFeatureFragment.class.getName();
	// inten.putExtra(PlaylistDetail.KEY_PLAYLIST_NAME, nameOfPlaylist);
	// Playlist playlist = (Playlist) mApp.listPlaylistFeature.get(position);
	// playlist.type = Playlist.TYPE_ONLINE;
	// mApp.tranportPlaylist.put(nameOfPlaylist, playlist);
	// mMainScreen.startActivity(inten);
	// }
	// };

	static class LoadAlbumFeature extends AsyncTask<Void, Void, Void> {
		WeakReference<ListPlaylistFeatureFragment> listPlaylistFragmentRfc;

		public LoadAlbumFeature(ListPlaylistFeatureFragment base) {
			listPlaylistFragmentRfc = new WeakReference<ListPlaylistFeatureFragment>(
					base);
		}

		private boolean checkActIsLive() {
			ListPlaylistFeatureFragment fragment = listPlaylistFragmentRfc
					.get();
			if (fragment == null) {
				return false;
			}
			if (fragment.mMainScreen instanceof ActivityBase) {
				if ((fragment.mMainScreen).isDestroy) {
					return false;
				}
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkActIsLive()) {
				return null;
			}
			ListPlaylistFeatureFragment fragment = listPlaylistFragmentRfc
					.get();
			JsonPlaylist jpl = JsonPlaylist.loadTopPlaylistList(0);
			if (jpl.isSuccess()) {
				fragment.mApp.listPlaylistFeature.clear();
				fragment.mApp.listPlaylistFeature.addAll(jpl.playlists);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkActIsLive()) {
				return;
			}

			ListPlaylistFeatureFragment fragment = listPlaylistFragmentRfc
					.get();
			fragment.setAdapter();
			fragment.showListPlaylist();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			if (!checkActIsLive()) {
				return;
			}
			ListPlaylistFeatureFragment fragment = listPlaylistFragmentRfc
					.get();
			fragment.showLoading();
		}
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		mMainScreen.trackPageView("Danh sách playlist chọn lọc");
	}
}
